﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

using System;
using System.Collections.Generic;
using System.Runtime.CompilerServices;
using JetBrains.Annotations;
using UnityEngine.Events;

public class ObjectPool<T> : IObjectPool
{
	private readonly UnityAction<T> m_ActionOnGet;
	private readonly UnityAction<T> m_ActionOnRelease;
	private CreateObj m_objCreator;
	private readonly Stack<T> m_Stack;

	public ObjectPool(CreateObj creator, UnityAction<T> actionOnGet, UnityAction<T> actionOnRelease)
	{
		this.m_Stack = new Stack<T>();
		this.m_objCreator = creator;
		this.m_ActionOnGet = actionOnGet;
		this.m_ActionOnRelease = actionOnRelease;
		ObjectPoolCache.s_AllPool.Add(this);
	}

	public T Get()
	{
		T local;
		if (this.m_Stack.Count == 0)
		{
			local = this.m_objCreator();
			this.countAll++;
		}
		else
		{
			local = this.m_Stack.Pop();
		}
		if (this.m_ActionOnGet != null)
		{
			this.m_ActionOnGet(local);
		}
		return local;
	}

	public void Release(T element)
	{
		if (element != null)
		{
			if (this.m_ActionOnRelease != null)
			{
				this.m_ActionOnRelease(element);
			}
			this.m_Stack.Push(element);
		}
	}

	public int countActive
	{
		get
		{
			return (this.countAll - this.countInactive);
		}
	}

	public int countAll { get; private set; }

	

	public int countInactive
	{
		get
		{
			return this.m_Stack.Count;
		}
		
	}
	
		

	public delegate T CreateObj();
}